package com.flyee.utils.load.contorl
{
	import com.flyee.utils.MyLog;
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.load.processor.LoadType;
	
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	public class LoaderTheardManager
	{
		private var loader:LoaderModel = null;
		private var returnFun:Function = null;
		private var progressFun:Function = null;
		private var loadingType:uint = 0;//0----等待加载响应，1---正在加载，2---加载完成
		public function LoaderTheardManager(loader:LoaderModel,returnFun:Function,progressFun:Function)
		{
			this.loader = loader;
			this.returnFun = returnFun;
			this.progressFun = progressFun;
			this.configureListeners(this.loader.contentLoaderInfo);
			this.loader.load(new URLRequest(this.loader.loadURL));
		}
		private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        }
		
		private function removeListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.removeEventListener(Event.COMPLETE, completeHandler);
            dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.removeEventListener(Event.INIT, initHandler);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.removeEventListener(Event.OPEN, openHandler);
            dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}
        private function completeHandler(event:Event):void 
        {
            MyLog.debug("completeHandler: " + event);
            if(!this.loader.needClose)
            {
            	this.loader.loadType = LoadType.LOADED_LOAD;
            	this.returnFun(this.loader)
            }
            else
            {
            	//this.loader.unloadAndStop(true);
            	this.loader.unload();
            }
            this.clear();
            
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void 
        {
            MyLog.debug("httpStatusHandler: " + event);
            if(this.loader.needClose)
            {
            	this.loader.close();
            	this.clear();
            }
        }

        private function initHandler(event:Event):void 
        {
            MyLog.debug("initHandler: " + event);
            if(this.loader.needClose)
            {
            	//this.loader.close();
            	this.clear();
            }
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
        {
        	this.loader.loadType = LoadType.ERROR_LOAD;
        	this.returnFun(this.loader)
            this.clear();
            MyLog.debug("ioErrorHandler: " + event);
        }

        private function openHandler(event:Event):void 
        {
            MyLog.debug("openHandler: " + event);
            if(this.loader.needClose)
            {
            	this.loader.close();
            	this.clear();
            }
        }

        private function progressHandler(event:ProgressEvent):void 
        {
            MyLog.debug("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
        	this.loader.loadType = LoadType.LOADING_LOAD;
        	this.progressFun(this.loader,event)
        	if(this.loader.needClose)
            {
            	this.loader.close();
            	this.clear();
            }
        }
		private function clear():void
		{
			 removeListeners(this.loader.contentLoaderInfo);
			 this.returnFun = null;
			 this.progressFun = null;
			 this.loader = null;
		}
	}
}